﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using snat.proxy.items;
using MySql.Data.MySqlClient;

namespace snat.proxy.sqlapi {
    public static class Query {

        public const string SELECT = "SELECT";
        public const string ALL = "*";
        public const string FROM = "FROM";
        public const string WHERE = "WHERE";
        public const string EQUALS = "=";
        public const string AS = "AS";
        public const string NATURAL_JOIN = "NATURAL JOIN";
        public const string FromEntityID = "FromEntityID";
        public const string ToEntityID = "ToEntityID";
        public const string EntityID = "EntityID";
        public const string EntityName = "EntityName";
        public const string Entity = "Entity";
        public const string Sender = "Sender";
        public const string Recipient = "Recipient";
        public const string MessageProperties = "MessageProperties";
        public const string MessageSend = "MessageSend";
        public const string MessageReceive = "MessageReceive";
        public const string Initial = "Initial";
        public const string ORDER_BY = "ORDER BY";
        public const string TimeStamp = "TimeStamp";
        public const string DOT = ".";
        public const string TRUE = "1";
        public const string FALSE = "0";
        public const string LIMIT = "LIMIT";
        public const string DESC = "DESC";

        public static ListProxy<T> SQLQuery<T>(string sqlQuery, bool isGreedy = false)
            where T : Item {
            return new ListProxy<T>(sqlQuery, isGreedy);
        }

        public static T ConstantQuery<T>(string sqlQuery, bool isGreedy = false)
            where T : Item {
                DataSet set = null;
                try {
                    set = DataProxy.Instance.RunQuery(sqlQuery);
                    Type type = typeof(T);
                    object[] parameters = new object[2];
                    parameters[0] = set.Tables["Table"].Rows[0];
                    parameters[1] = isGreedy;
                    return (T)Activator.CreateInstance(type, parameters);
                } catch (MySqlException ex) {
                    throw ex;
                }
        }
    }
}